/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Diagnostics;
using System.Globalization;

namespace Xceed.Silverlight.Data
{
  public class DataItem
  {
    public DataItem()
      : this( null, false )
    {
    }

    public DataItem( object data )
      : this( data, true )
    {
    }

    internal DataItem( object data, bool isLoaded )
    {
#if DEBUG
#endif

      m_isLoaded = isLoaded;
      m_data = data;
    }


    #region IsLoaded Property

    public bool IsLoaded
    {
      get
      {
        return m_isLoaded;
      }
    }

    private bool m_isLoaded; //false

    #endregion

    #region Data Property

    public object Data
    {
      get
      {
        return m_data;
      }
    }

    private object m_data; //null

    #endregion

    public override int GetHashCode()
    {
      if( m_isLoaded )
        return ( m_data != null )
                 ? m_data.GetHashCode()
                 : 0;

      if( !m_forcedHash.HasValue )
      {
        m_forcedHash = base.GetHashCode();
      }

      return m_forcedHash.Value;
    }

    public override bool Equals( object obj )
    {
      if( object.ReferenceEquals( obj, this ) )
        return true;

      DataItem dataItem = obj as DataItem;

      // Two different DataItem instance are consider equals if they are
      // both loaded and contain an equal data object.  A DataItem that is
      // empty or not loaded must not be equal to any other DataItem except
      // itself in order to represent different virtualized items.
      return ( m_isLoaded )
          && ( !object.ReferenceEquals( dataItem, null ) )
          && ( dataItem.m_isLoaded )
          && ( object.Equals( dataItem.m_data, m_data ) );
    }

#if DEBUG

    public override string ToString()
    {
      string data = ( this.Data == null ) ? "#NULL#" : this.Data.ToString();
      return (this.IsLoaded)
        ? data
        : data + " (not loaded)";
    }
#endif


    #region Private Fields

    private Nullable<int> m_forcedHash = null;

    #endregion Private Fields
  }
}
